﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesRaster;

namespace DIWAGIS.Class
{
    public class clsTuongTacBanDo
    {
        /// <summary>
        /// Phóng to bản đồ
        /// </summary>
        /// <param name="mapControl"></param>
        /// HungNQ
        public void ZoomIn(IMapControl4 mapControl)
        {
            try
            {
                ICommand toolZoomIn = new ControlsMapZoomInToolClass();
                toolZoomIn.OnCreate(mapControl.Object);
                mapControl.CurrentTool = toolZoomIn as ITool;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Thu nhỏ bản đồ
        /// </summary>
        /// <param name="mapControl"></param>
        /// Hưng NQ
        public void ZoomOut(IMapControl4 mapControl)
        {
            try
            {
                ICommand toolZoomOut = new ControlsMapZoomOutToolClass();
                toolZoomOut.OnCreate(mapControl.Object);
                mapControl.CurrentTool = toolZoomOut as ITool;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Dịch chuyển bản đồ
        /// </summary>
        /// <param name="mapControl"></param>
        /// HungNQ
        public void PanMap(IMapControl4 mapControl)
        {
            try
            {
                ICommand toolPan = new ControlsMapPanToolClass();
                
                toolPan.OnCreate(mapControl.Object);
                mapControl.CurrentTool = toolPan as ITool;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Dịch chuyển bản đồ
        /// </summary>
        /// <param name="mapControl"></param>
        /// HungNQ
        public void ZoomFullExtent(IMapControl4 mapControl)
        {
            try
            {
                ICommand cmdZoomFullExtent = new ControlsMapFullExtentCommandClass();
                cmdZoomFullExtent.OnCreate(mapControl.Object);
                cmdZoomFullExtent.OnClick();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Back Extent
        /// </summary>
        /// <param name="mapControl"></param>
        /// HungNQ
        public void PreviousExtent(IMapControl4 mapControl)
        {
            try
            {
                ICommand cmdPreviousExtent = new ControlsMapZoomToLastExtentBackCommandClass();
                cmdPreviousExtent.OnCreate(mapControl.Object);
                cmdPreviousExtent.OnClick();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void EditCutCommand(IMapControl4 mapControl)
        {
            try
            {
                ICommand cmdEditCut = new ControlsEditingCutCommand();
                cmdEditCut.OnCreate(mapControl.Object);
                cmdEditCut.OnClick();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Next Extent
        /// </summary>
        /// <param name="mapControl"></param>
        /// HungNQ
        public void NextExtent(IMapControl4 mapControl)
        {
            try
            {
                ICommand cmdNextExtent = new ControlsMapZoomToLastExtentForwardCommandClass();
                cmdNextExtent.OnCreate(mapControl.Object);
                cmdNextExtent.OnClick();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Select Features
        /// </summary>
        /// <param name="mapControl"></param>
        /// HungNQ
        public void SelectFeatures(IMapControl4 mapControl)
        {
            try
            {
                ICommand toolSelectFeatures = new ControlsSelectFeaturesToolClass();
                toolSelectFeatures.OnCreate(mapControl.Object);
                mapControl.CurrentTool = toolSelectFeatures as ITool;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Clear Selection
        /// </summary>
        /// <param name="mapControl"></param>
        /// HungNQ
        public void ClearSelection(IMapControl4 mapControl)
        {
            try
            {
                ICommand cmdSelectFeatures = new ControlsClearSelectionCommandClass();
                cmdSelectFeatures.OnCreate(mapControl.Object);
                cmdSelectFeatures.OnClick();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Release Current Tool
        /// </summary>
        /// <param name="mapControl"></param>
        /// HungNQ
        public void ReleaseCurrentTool(IMapControl4 mapControl)
        {
            try
            {
                mapControl.CurrentTool = null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Nhấp nháy đối tượng
        /// </summary>
        /// <param name="geometry"></param>
        /// <param name="color"></param>
        /// <param name="display"></param>
        /// <param name="delay"></param>
        public void FlashGeometry(IMapControl4 MapControl, ESRI.ArcGIS.Geometry.IGeometry geometry, ESRI.ArcGIS.Display.IRgbColor color, ESRI.ArcGIS.Display.IDisplay display, int nFlashs, System.Int32 delay)
        {
            if (geometry == null || color == null || display == null)
            {
                return;
            }
            display.StartDrawing(display.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast

            switch (geometry.GeometryType)
            {
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
                    {
                        //Set the flash geometry's symbol.
                        ESRI.ArcGIS.Display.ISimpleFillSymbol simpleFillSymbol = new ESRI.ArcGIS.Display.SimpleFillSymbolClass();
                        simpleFillSymbol.Color = color;
                        ESRI.ArcGIS.Display.ISymbol symbol = simpleFillSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic Cast
                        symbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;
                        //Flash the input polygon geometry.
                        display.SetSymbol(symbol);
                        MapControl.FlashShape(geometry, nFlashs, delay, symbol);
                        //display.DrawPolygon(geometry);
                        //System.Threading.Thread.Sleep(delay);
                        //display.DrawPolygon(geometry);
                        break;
                    }
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
                    {
                        //Set the flash geometry's symbol.
                        ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
                        simpleLineSymbol.Width = 4;
                        simpleLineSymbol.Color = color;
                        ESRI.ArcGIS.Display.ISymbol symbol = simpleLineSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic Cast
                        symbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;
                        //Flash the input polyline geometry.
                        display.SetSymbol(symbol);
                        MapControl.FlashShape(geometry, nFlashs, delay, symbol);
                        //display.DrawPolyline(geometry);
                        //System.Threading.Thread.Sleep(delay);
                        //display.DrawPolyline(geometry);
                        break;
                    }
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
                    {
                        //Set the flash geometry's symbol.
                        ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
                        simpleMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle;
                        simpleMarkerSymbol.Size = 12;
                        simpleMarkerSymbol.Color = color;
                        ESRI.ArcGIS.Display.ISymbol symbol = simpleMarkerSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic Cast
                        symbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;
                        //Flash the input point geometry.
                        display.SetSymbol(symbol);
                        MapControl.FlashShape(geometry, nFlashs, delay, symbol);
                        //display.DrawPoint(geometry);
                        //System.Threading.Thread.Sleep(delay);
                        //display.DrawPoint(geometry);
                        break;
                    }
                case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryMultipoint:
                    {
                        //Set the flash geometry's symbol.
                        ESRI.ArcGIS.Display.ISimpleMarkerSymbol simpleMarkerSymbol = new ESRI.ArcGIS.Display.SimpleMarkerSymbolClass();
                        simpleMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSCircle;
                        simpleMarkerSymbol.Size = 12;
                        simpleMarkerSymbol.Color = color;
                        ESRI.ArcGIS.Display.ISymbol symbol = simpleMarkerSymbol as ESRI.ArcGIS.Display.ISymbol; // Dynamic Cast
                        symbol.ROP2 = ESRI.ArcGIS.Display.esriRasterOpCode.esriROPNotXOrPen;
                        //Flash the input multipoint geometry.
                        display.SetSymbol(symbol);
                        MapControl.FlashShape(geometry, nFlashs, delay, symbol);
                        //display.DrawMultipoint(geometry);
                        //System.Threading.Thread.Sleep(delay);
                        //display.DrawMultipoint(geometry);
                        break;
                    }
            }
            display.FinishDrawing();
        }

        /// <summary>
        /// Thêm file cad vào bản đồ
        /// </summary>
        /// <param name="MapControl"></param>
        /// <param name="strDirectoryPath"></param>
        /// <param name="strFileName"></param>
        public void AddCadFileToMap(IMapControl4 MapControl, string strDirectoryPath, string strFileName)
        {

            //Tạo group layer
            IGroupLayer groupLayer = new GroupLayerClass();
            groupLayer.Name = System.IO.Path.GetFileNameWithoutExtension(strDirectoryPath + "\\" + strFileName);

            IWorkspaceFactory pCadworkspacefactory = new CadWorkspaceFactoryClass();
            IFeatureLayer fLayer;
            //Mở workspace
            IFeatureWorkspace pWorkspace = pCadworkspacefactory.OpenFromFile(strDirectoryPath, 0) as IFeatureWorkspace;
            //Mở dataset
            IFeatureDataset pFeatureDataset = pWorkspace.OpenFeatureDataset(strFileName);
            //Lấy feature class trong dataset
            IFeatureClassContainer pFeatureClassContainer = pFeatureDataset as IFeatureClassContainer;
            for (int i = 0; i < pFeatureClassContainer.ClassCount; i++)
            {
                IFeatureClass fc = pFeatureClassContainer.get_Class(i);
                if ((fc.FeatureType == esriFeatureType.esriFTAnnotation) || (fc.FeatureType == esriFeatureType.esriFTCoverageAnnotation))
                {
                    fLayer = new CadAnnotationLayerClass();
                }
                else
                {
                    fLayer = new FeatureLayerClass();
                }
                fLayer.FeatureClass = fc;
                fLayer.Name = fc.AliasName;
                groupLayer.Add(fLayer);
            }
            MapControl.AddLayer(groupLayer, 0);
        }

        /// <summary>
        /// Thêm ảnh vào bản đồ
        /// </summary>
        /// <param name="MapControl"></param>
        /// <param name="strDirectoryPath"></param>
        /// <param name="strFileName"></param>
        public void AddRasterToMap(IMapControl4 MapControl, string strDirectoryPath, string strFileName)
        {
            IWorkspaceFactory pWSF = new RasterWorkspaceFactoryClass();
            IRasterWorkspace pRWS = pWSF.OpenFromFile(strDirectoryPath, 0) as IRasterWorkspace;
            IRasterDataset pRDS = pRWS.OpenRasterDataset(strFileName);
            IRasterLayer pRL = new RasterLayerClass();
            pRL.Name = System.IO.Path.GetFileNameWithoutExtension(strDirectoryPath + "\\" + strFileName);
            pRL.CreateFromDataset(pRDS);
            MapControl.AddLayer(pRL as ILayer, 0);
        }
    }
}